/*
 * AutoCompleteAdapter.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.indexeddictionary;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;

import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import de.studiorutton.colorschemes.IColorSchemeMapper;
import de.studiorutton.gerengdic.Prefs;
import de.studiorutton.gerengdic.R;

public class AutoCompleteAdapter extends BaseAdapter {
	private static final String TAG = "learningwords";
	private ArrayList<AutoCompleteRecord> data;
	private IIndexedDictionaryView idl;
	private int lastMaxError;

	public AutoCompleteAdapter(IIndexedDictionaryView idl) {
		this.idl = idl;
		data = new ArrayList<AutoCompleteRecord>();
		lastMaxError = 0;
	}

	public void addItem(AutoCompleteRecord item) {
		data.add(item);
		if (item.errors > lastMaxError) {
			AutoCompleteRecord acr = new AutoCompleteRecordHeadline(item.errors);
			data.add(acr);
			lastMaxError = item.errors;
		}
	}

	public void sortItems() {
		Collections.sort(data, new AutoCompleteRecord.ACRComparator());
	}

	public void clear(boolean invalidate) {
		data.clear();
		lastMaxError = 0;
		if (invalidate) {
			notifyDataSetInvalidated();
		}
	}

	static class ViewHolder {
		RelativeLayout layout;
		TextView text;
		ImageView image;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position > getCount())
			return null;

		IColorSchemeMapper csmi = Prefs.getInstance().getColorSchemeMapper();

		// Log.d(TAG, "AutoCompleteAdapter: getView " + position + ".");
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(idl.getContext()).inflate(
					R.layout.indexeddictionaryacitem, parent, false);
			holder = new ViewHolder();
			holder.layout = (RelativeLayout) convertView
					.findViewById(R.id.autocompleteitem);
			holder.text = (TextView) convertView
					.findViewById(R.id.autocompletetext);
			holder.image = (ImageView) convertView.findViewById(R.id.arrow);
			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();

		if (data.get(position).type == AutoCompleteRecord.TYPE_HEADLINE) {
			int sId;
			if (data.get(position).errors == 1)
				sId = R.string.autocomplete_found_mismatch;
			else
				sId = R.string.autocomplete_found_mismatches;

			String s = idl.getResources().getString(sId);
			holder.text.setText(MessageFormat.format(s,
					data.get(position).errors));
			holder.text.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
			holder.text.setTextColor(csmi.getResultContentInsideTextcolor());
			holder.layout.setBackgroundDrawable(csmi
					.getResultContentInsideBackground());
			holder.image.setImageDrawable(null);
		} else {
			holder.text.setText(data.get(position).getKey());
			holder.text.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);

			if (position % 2 == 0) {
				holder.text.setTextColor(csmi.getResultContentTextcolorOne());
				holder.layout.setBackgroundDrawable(csmi
						.getResultContentBackgroundOne());
				holder.image.setImageDrawable(csmi.getResultContentArrowOne());
			} else {
				holder.text.setTextColor(csmi.getResultContentTextcolorTwo());
				holder.layout.setBackgroundDrawable(csmi
						.getResultContentBackgroundTwo());
				holder.image.setImageDrawable(csmi.getResultContentArrowTwo());
			}
		}

		return convertView;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		return data.get(position).type;
	}

	@Override
	public int getViewTypeCount() {
		return 2; // Number of AutocompleteRecord.TYPES_
	}
	
	@Override
	public boolean isEnabled(int position) {
		return data.get(position).type != AutoCompleteRecord.TYPE_HEADLINE;
	}

}